package com.kjjt.controller;

import com.kjjt.vo.DemoVo;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.nio.charset.Charset;

@Validated
@RestController
@RequestMapping("/demo")
public class TestController {

    @GetMapping("/demo001")
    public String test(String str, Integer i) {
        return "GET " + "str: " + str + " -- i: " + i;
    }

    @PostMapping("/demo001")
    public String test1(String str, Integer i) {
        return "POST " + "str: " + str + " -- i: " + i;
    }

    @PostMapping(value = "/demo002")
    public String test2(@RequestParam(value = "str111", defaultValue = "未填写") String str, Integer i) {
        return "POST " + "str: " + str + " -- i: " + i;
    }

    @PostMapping(value = "/demo003/{str}/post")
    public String test3(@PathVariable String str) {
        return str;
    }

    @PostMapping(value = "/demo004")
    public String test4(DemoVo demoVo) {
        return demoVo.getName() + " | " + demoVo.getAge();
    }

    @PostMapping(value = "/demo005")
    public String test5(@RequestBody DemoVo demoVo) {
        return demoVo.getName() + " | " + demoVo.getAge();
    }

    @PostMapping(value = "/demo006")
    public String test6(MultipartFile file) {
        try {
            return StreamUtils.copyToString(file.getInputStream(), Charset.defaultCharset());
        } catch (Exception e) {
            return "文件读取失败！";
        }
    }

    @PostMapping("/demo007")
    public String test7(@NotBlank String str) {
        return str;
    }

    @PostMapping(value = "/demo008")
    public String test8(@RequestBody @Valid DemoVo demoVo) {
        return demoVo.getName() + " | " + demoVo.getAge();
    }

}
